class Solution {
public:
    int maxTurbulenceSize(vector<int>& a) {
        int n=a.size();
        vector<int> f(n);
        vector<int> g(n);
        f[0]=1;
        g[0]=1;
        int ret=0;
        if(n<2) return n;
        for(int i=1;i<n;++i)
        {
            if(a[i-1]>a[i])
                f[i]=1;
            else if(a[i-1]==a[i])
                f[i]=1;
            else
                f[i]=g[i-1]+1;

            if(a[i-1]>a[i])
                g[i]=f[i-1]+1;
            else if(a[i-1]==a[i])
                g[i]=1;
            else
                g[i]=1;
            ret=max(ret,max(g[i],f[i]));
        }    
        return ret;
    }
};
